package vn.codespot.hms.api;

import java.io.IOException;

import vn.codespot.hms.model.request.ApplyRequestModel;
import vn.codespot.hms.model.request.ConfirmRequestModel;
import vn.codespot.hms.model.request.FeesRequestModel;
import vn.codespot.hms.model.request.StatusRequestModel;
import vn.codespot.hms.model.response.ApplyResponseModel;
import vn.codespot.hms.model.response.ConfirmResponseModel;
import vn.codespot.hms.model.response.FeesRepsonseModel;
import vn.codespot.hms.model.response.NationalResponseModel;
import vn.codespot.hms.model.response.StatusResponseModel;
import vn.codespot.hms.utils.LogUtil;
import android.text.TextUtils;

public class Service {
    public static final String API = "visa_api.php";

    public static final String HOST = "http://api.vietnamvisaform.com";

    public static final String SERVER_ADDR = HOST + "/" + API;

    private static String mServer;

    private final Requester mRequester;

    public Service() {
        mRequester = new Requester();
        mServer = SERVER_ADDR;
    }

    public static void setServerUrl(String url) throws IllegalArgumentException {
        if (TextUtils.isEmpty(url)) {
            throw new IllegalArgumentException("URL must be neither NULL nor empty.");
        } else if (!url.startsWith("http://") || !url.startsWith("https://")) {
            throw new IllegalArgumentException("Invalid URL.");
        }
        if (url.endsWith("/")) {
            mServer = url.substring(0, url.lastIndexOf('/'));
        } else {
            mServer = url;
        }
    }
    
    public ApplyResponseModel postApply(ApplyRequestModel request) throws IllegalArgumentException {
        if (request == null) {
            throw new IllegalArgumentException("Request data for posting apply is required.");
        }
        String url = mServer + "/visa/apply";
        try {
            return (ApplyResponseModel) mRequester.doPost(url, request);
        } catch (IOException e) {
            LogUtil.e("IOException: ", e);
            return null;
        }
    }
    
    public ConfirmResponseModel postConfirm(ConfirmRequestModel request) throws IllegalArgumentException {
        if (request == null) {
            throw new IllegalArgumentException("Request data for posting apply is required.");
        }
        String url = mServer + "/visa/confirm";
        try {
            return (ConfirmResponseModel) mRequester.doPost(url, request);
        } catch (IOException e) {
            LogUtil.e("IOException: ", e);
            return null;
        }
    }

    public FeesRepsonseModel getFees(FeesRequestModel request) throws IllegalArgumentException {
        if (request == null) {
            throw new IllegalArgumentException("Request data for getting fees is required.");
        }
        String url = mServer + "/visa/fees";
        try {
            return (FeesRepsonseModel) mRequester.doGet(url, request);
        } catch (IOException e) {
            LogUtil.e("IOException: ", e);
            return null;
        }
    }

    public StatusResponseModel getStatus(StatusRequestModel request) throws IllegalArgumentException {
        if (request == null) {
            throw new IllegalArgumentException("Request data for getting status is required.");
        }
        String url = mServer + "/visa/status";
        try {
            return (StatusResponseModel) mRequester.doGet(url, request);
        } catch (IOException e) {
            LogUtil.e("IOException: ", e);
            return null;
        }
    }

    public NationalResponseModel getNational() {
        String url = mServer + "/national";
        try {
            return (NationalResponseModel) mRequester.doGet(url, null);
        } catch (IOException e) {
            LogUtil.e("IOException: ", e);
            return null;
        }
    }

    public void cancelRequesting() {
        mRequester.forceCancelHttpRequest();
    }
}
